<?php

/* 访问控制 */
defined('IN_ECTOUCH') or die('Deny Access');

class IndexController extends CommonController {

    /**
     * 首页信息
     */
    public function index() {
        // 自定义导航栏
        $navigator = model('Common')->get_navigator();
        $this->assign('navigator', $navigator['middle']);

		//新品首发、热销机型
        $this->assign('new_goods', model('Index')->goods_list('new',6,0,425));
        $this->assign('hot_goods', model('Index')->goods_list('hot',6,0,425));

		// rg808猜你喜欢的
		$this->assign('cainixihuan', get_cainixihuan());

		$news_public = model('ArticleBase')->get_cat_articles(12, 1, 1);
		$this->assign('news_public', $news_public);

        $this->display('index.dwt');
    }

    /**
     * ajax获取商品
     */
    public function ajax_goods() {
        if (IS_AJAX) {
            $type = I('get.type');
            $start = $_POST['last'];
            $limit = $_POST['amount'];
            $hot_goods = model('Index')->goods_list($type, $limit, $start);
            $list = array();
            // 热卖商品
            if ($hot_goods) {
                foreach ($hot_goods as $key => $value) {
                    $this->assign('hot_goods', $value);
                    $list [] = array(
                        'single_item' => ECTouch::view()->fetch('library/asynclist_index.lbi')
                    );
                }
            }
            echo json_encode($list);
            exit();
        } else {
            $this->redirect(url('index'));
        }
    }

	function get_ipLoc(){
		$cityname = isset($_REQUEST['cityname']) ? substr($_REQUEST['cityname'], 0, strpos($_REQUEST['cityname'], '市')) : '南宁';
		$regions_data = $this->model->table("region")->where("region_name like '".$cityname."%'")->find();
		$region_id = $this->model->table("region")->where("parent_id = '".$regions_data['region_id']."'")->field("region_id")->getOne();
		if( $region_id && !$this->rg_ipLoc ){
			setcookie('rg_ipLoc', $regions_data['parent_id'].'-'.$regions_data['region_id'].'-'.$region_id );

			die(json_encode( $regions_data['parent_id'].'-'.$regions_data['region_id'].'-'.$region_id ));
		}else{
			die(json_encode( $this->rg_ipLoc ));
		}
	}
}
